package Q6;

import java.util.*;

public class Question6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 输入占用分区的进程数量
        int n = Integer.parseInt(scanner.nextLine().trim());

        // 输入已占用的起始地址和存储容量
        String[] usedMemoryInfo = scanner.nextLine().trim().split(" ");
        List<Process> allocatedProcesses = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            int startAddress = Integer.parseInt(usedMemoryInfo[2 * i]);
            int size = Integer.parseInt(usedMemoryInfo[2 * i + 1]);
            allocatedProcesses.add(new Process("p" + (i + 1), startAddress, size));
        }

        // 输入回收的进程
        String[] reclaimedProcesses = scanner.nextLine().trim().split(" ");
        Set<String> reclaimSet = new HashSet<>(Arrays.asList(reclaimedProcesses));

        // 检查回收的进程是否存在，创建空闲分区列表
        List<FreePartition> freePartitions = new ArrayList<>();
        Map<String, Process> processMap = new HashMap<>();

        for (Process process : allocatedProcesses) {
            processMap.put(process.id, process);
        }

        for (String id : reclaimSet) {
            if (!processMap.containsKey(id)) {
                System.out.println("false");
                return;
            }
        }

        // 回收进程
        for (String id : reclaimSet) {
            Process process = processMap.get(id);
            allocatedProcesses.remove(process);
        }

        // 创建空闲分区
        int totalMemory = 512; // Total memory = 512KB
        int lastEnd = 0;

        for (Process process : allocatedProcesses) {
            int startAddress = process.startAddress;
            if (lastEnd < startAddress) {
                freePartitions.add(new FreePartition(lastEnd, startAddress - lastEnd));
            }
            lastEnd = process.startAddress + process.size;
        }

        if (lastEnd < totalMemory) {
            freePartitions.add(new FreePartition(lastEnd, totalMemory - lastEnd));
        }

        // 输出空闲分区的个数
        System.out.println(freePartitions.size());
    }
}

class Process {
    String id;
    int startAddress;
    int size;

    Process(String id, int startAddress, int size) {
        this.id = id;
        this.startAddress = startAddress;
        this.size = size;
    }
}

class FreePartition {
    int startAddress;
    int size;

    FreePartition(int startAddress, int size) {
        this.startAddress = startAddress;
        this.size = size;
    }
}